package chapter11

/**
  * @Copyright: Shanghai Definesys Company.All rights reserved.
  * @Description:
  * @author: chuhaitao
  * @since: 2019/1/13 20:08
  * @history:
  *          1.2019/1/13 created by chuhaitao
  */
object MapDemo {

  def main(args: Array[String]): Unit = {
    //默认的是不可变， K V是任何类型
    //底层 每一对的K-V是Tuple2
    var map01 = Map(1 -> 21, "12" -> 1)
    println("map01:" + map01)


    import scala.collection.mutable
    //可变的map,无需
    var map02 = mutable.Map("A" -> 1, "B" -> 2, "C" -> 3)

    println("MAP02:" + map02)

    //空的map
    var map03 = mutable.Map

    println("map03:" + map03)

    //对偶元组   二元组

    var map04 = mutable.Map(("a", 1), ("b", 2))
    println("map04:" + map04)


    if (map04.contains("a")) {

      println("map04  a :" + map04("a"))

    }

    //3


    println("map04  get b :" + map04.get("a"))


    println("map04  getOrElse:" + map04.getOrElse("af", "12121"))


    var map05 = mutable.Map("t" -> 1, "g" -> 2)

    map05("t") = 12
    map05+=("t"->122)

    println("map05:"+map05("t"))

    map05.remove("t")
    map05-=("t")
    println("map05:"+map05)





    var map06 = mutable.Map("t" -> 1, "g" -> 2,"y"->21)
    //方式1
    for ((k,v)<- map06){
      println("k:"+k+",v："+v)
    }
    //方式2 只取出v
    for (v<- map06.values){
      println("v："+v)
    }

    //方式3 只取出v
    for (k<- map06.keys){
      println("k："+k)
    }

    //方式4 遍历成元组
    for (t<- map06){
      println("t："+t)
    }
  }
}
